package com.example.dzbsh.util;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

public class CreateWord {

	public static void createWord(String userfilepath,String filepath,String filename,Map<String,Object> dataMap,
			HttpServletRequest request,HttpServletResponse response,String ppFlag,String ppShifoufanhuiwenjian){
		DocUtil doc = new DocUtil();
		Writer out = null;
		try {
			File dir = new File(filepath);
			if(!dir.exists()){
				dir.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(filepath + filename);
			out = new OutputStreamWriter(fos,"utf-8");
			String mmMuBanMingCheng="";
			String templatepath = "";
			
			if ("js".equals(ppFlag)) {
				mmMuBanMingCheng="jingshanghuizong.xml";
				templatepath =  "./userfiles/muban/";
			}
			if ("zb".equals(ppFlag)) {
				mmMuBanMingCheng="zhenbiehuizong.xml";
				templatepath ="./userfiles/muban/";
			}
			if ("gf".equals(ppFlag)) {
				mmMuBanMingCheng="guifanhuizong.xml";
				templatepath ="./userfiles/muban/";
			}
			if ("jstj".equals(ppFlag)) {
				mmMuBanMingCheng="jingshangtongji.xml";
				templatepath ="./userfiles/muban/";
			}
			if ("rd".equals(ppFlag)) {
				mmMuBanMingCheng="zhenbierending.xml";
				templatepath ="./userfiles/muban/";
			}
			if ("gftj".equals(ppFlag)) {
				mmMuBanMingCheng="guifantongji.xml";
				templatepath ="./userfiles/muban/";
			}
			doc.write(templatepath, mmMuBanMingCheng, dataMap, out);
			if (("1").equals(ppShifoufanhuiwenjian)) {
				reutrnfile(filepath,filename,request,response);

			}
		} catch (Exception e) {
			throw new RuntimeException("createWord"+e.getMessage());
		}
	}
	
	public static void createXLS(String userfilepath,String filepath,String filename,String ppMubanName,Map<String,Object> dataMap,
			HttpServletRequest request,HttpServletResponse response){
		DocUtil doc = new DocUtil();
		Writer out = null;
		try {
			File dir = new File(filepath);
			if(!dir.exists()){
				dir.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(filepath + filename);
			out = new OutputStreamWriter(fos,"utf-8");
			String mmMuBanMingCheng="";
			String templatepath = "";

			templatepath = "./userfiles/muban/";

			doc.write(templatepath, ppMubanName, dataMap, out);
			reutrnfile(filepath,filename,request,response);

		} catch (Exception e) {
			throw new RuntimeException("createWord"+e.getMessage());
		}
	}
	
	public static  void  reutrnfile(String filepath,String filename,
			HttpServletRequest request,HttpServletResponse response) throws IOException {
		ServletOutputStream out = null;
		FileInputStream ips = null;
		try {
			
			// 获取文件存放的路径
			File file = new File(filepath + filename);
			String newFileName = filename;
			if (!file.exists()) {
				// 如果文件不存在就跳出
				return ;
			}
			ips = new FileInputStream(file);

			response.setContentType("multipart/form-data");

			// 为文件重新设置名字，采用数据库内存储的文件名称
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ newFileName
					+ "\"");

			out = response.getOutputStream();

			// 读取文件流
			int len = 0;
			byte[] buffer = new byte[1024 * 10];
			while ((len = ips.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}

			out.flush();

			ips.close();

			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
